import { NextRequest, NextResponse } from "next/server";
import prisma from "@/lib/prisma";

export async function POST( request: NextRequest ) {
    const body = await request.json()
    const {id, ...data} = body
    if(!data.chatId) {
        const chat = await prisma.chat.create({ //創建新的對話放
            data: {
                title: '新對話'
            }
        })
        data.chatId = chat.id
    }
    let message = await prisma.message.upsert({
        create:data,
        update:data,
        where:{
            id
        }
    })
    // if(id) {
    //     message = await prisma.message.update({
    //         where: {
    //             id
    //         },
    //         data
    //     })
    // } else {
    //     message = await prisma.message.create({
    //         data
    //     })
    // }
    return NextResponse.json({code: '00', data: {message}})
}